package st.business.tepth.business.goodsorder;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.business.bean.MaterialHadApplyBean;
import st.business.tepth.business.event.GoodsEvent;
import st.business.tepth.business.goodsorder.entity.Level0;
import st.business.tepth.business.goodsreceive.GoodsReceiveConvert;
import st.business.tepth.debug.DataProvider;
import st.business.tepth.manager.UserManager;
import st.shadow.library.core.base.mvp.MvpFragment;
import st.shadow.library.ui.dialog.DialogLoader;
import st.shadow.library.ui.dialog.IDialogClickListener;
import st.shadow.library.ui.recycler.BaseDecoration;

import static st.business.tepth.business.goodsorder.GoodsOrderAdapter.TYPE_LEVEL_0;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-02-22 17:30
 * 领料单
 */

public class GoodsOrderDelegate extends MvpFragment<GoodsOrderPresenter> implements GoodsOrderContract.View {
    public static GoodsOrderDelegate newInstance(int id) {
        Bundle bundle = new Bundle();
        bundle.putInt("repairId", id);
        GoodsOrderDelegate delegate = new GoodsOrderDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R2.id.tv_tile)
    TextView mTvTitle;
    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    private GoodsOrderAdapter mOrderAdapter;
    private DialogLoader mDialogLoader;
    private int mRepairId;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mRepairId = bundle.getInt("repairId");
        }
    }

    @Override
    public GoodsOrderPresenter getPresenter() {
        return new GoodsOrderPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_goods_order;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mTvTitle.setText("已申请领料单");
        mRecyclerView.setLayoutManager(new GridLayoutManager(_mActivity, 2));
        mRecyclerView.addItemDecoration(BaseDecoration.
                create(Color.parseColor("#f0f0f0"), 1));
        mOrderAdapter = new GoodsOrderAdapter(DataProvider.getGoodsOrderList());
        mOrderAdapter.setSpanSizeLookup(new BaseQuickAdapter.SpanSizeLookup() {
            @Override
            public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
                MultiItemEntity entity = mOrderAdapter.getItem(position);
                if (entity != null) {
                    return entity.getItemType() == TYPE_LEVEL_0 ? 2 : 1;
                } else {
                    return 2;
                }
            }
        });
        mRecyclerView.setAdapter(mOrderAdapter);
        mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                Level0 lv0 = (Level0) adapter.getItem(position);
                if (lv0 != null) {
                    MaterialHadApplyBean goodsOrderBean = lv0.getGoodsOrderBean();
                    if (goodsOrderBean != null) {
                        if (view.getId() == R.id.tv_operation) {
                            doOperation(goodsOrderBean);
                        } else if (view.getId() == R.id.tv_re_apply) {
                            reApplyGoods(goodsOrderBean);
                        }
                    }
                }
            }
        });
        mDialogLoader = new DialogLoader(_mActivity);
        mOrderAdapter.setEmptyView(R.layout.view_loading,
                (ViewGroup) mRecyclerView.getParent());
        mPresenter.getData(UserManager.getInstance().getToken(),mRepairId);
    }

    @OnClick({R2.id.tv_back})
    public void onBack() {
        pop();
    }

    @Override
    public void showData(List<MaterialHadApplyBean> list) {
        mOrderAdapter.setEmptyView(R.layout.view_search_empty, (ViewGroup) mRecyclerView.getParent());
        mOrderAdapter.setNewData(new GoodsOrderConvert().convertToEntity(list));
    }

    @Override
    public void showError(String msg) {
        if (mDialogLoader != null) {
            mDialogLoader.stopProgressWithFailed(msg, msg);
        }
    }

    @Override
    public void showSucceed(final String msg) {
        if (mDialogLoader != null) {
            _mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mDialogLoader.stopProgressWithSuccess(msg, null, new IDialogClickListener() {
                        @Override
                        public void onClick() {
                            mDialogLoader.clearDialog();
                        }
                    });
                }
            });
        }
        EventBus.getDefault().post(new GoodsEvent());
    }

    private void doOperation(MaterialHadApplyBean bean) {
        switch (bean.getStatus()) {
            case 1:
            case 2:
                cancelWaitApprove(bean);
                break;
            case 3:
            case 5:
            case 8:
            case 9:
                returnGoods(bean);
                break;
        }
    }

    /**
     * 退料
     */
    private void returnGoods(MaterialHadApplyBean bean) {
//        DialogReturnGoods dialogReturnGoods = new DialogReturnGoods(_mActivity,
//                bean.getParts(), new OnItemChooseListener<String>() {
//            @Override
//            public void onChoose(String s) {
        mDialogLoader.showProgressDialog(false, "正在提交",
                "请稍候...");
        mPresenter.returnGoods(UserManager.getInstance().getToken(),
                new GoodsOrderConvert().convertToIDBeanString(bean));
//            }
//        });
//        Window dialogWindow = dialogReturnGoods.getWindow();
//        if (dialogWindow != null) {
//            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//            lp.width = ScreenUtils.getScreenWidth() * 1000 / 1280; // 宽度
//            lp.height = ScreenUtils.getScreenHeight() * 720 / 1024; // 高度
//            dialogWindow.setGravity(Gravity.CENTER);
//            dialogWindow.setAttributes(lp);
//            dialogReturnGoods.show();
//        }
    }

    /**
     * 重新申请
     */
    public void reApplyGoods(MaterialHadApplyBean bean) {
        mDialogLoader.showProgressDialog(false, "正在提交",
                "请稍候...");
        mPresenter.reApplyOrder(UserManager.getInstance().getToken(),
                new GoodsReceiveConvert()
                        .convertToIDBeanString(bean), bean.getStatus());
    }

    /**
     * 取消待审批
     */
    private void cancelWaitApprove(MaterialHadApplyBean bean) {
        mDialogLoader.showProgressDialog(false, "正在提交",
                "请稍候...");
        mPresenter.cancelOrder(UserManager.getInstance().getToken(),
                new GoodsReceiveConvert()
                        .convertToIDBeanString(bean), bean.getStatus());
    }
}